package ar.com.tristeslos3tigres.rssfeedreader;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import ar.com.tristeslos3tigres.rssfeedreader.RssFeedItem.RssFeedTextItemType;

public class RssFeedAdapter extends BaseAdapter {
	private Context context;
	private List<RssFeedItem> data;
	private LayoutInflater inflater = null;

	/**
	 * @param context
	 * @param data
	 */
	public RssFeedAdapter(Context context, List<RssFeedItem> data) {
		super();
		this.context = context;
		this.data = data;
		this.inflater = (LayoutInflater) this.context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		return data.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return data.get(arg0).hashCode();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		RssFeedItem rssFeedItem = data.get(position);

		if (vi == null) {
			
			Log.v("RssFeedAdapter",
				  "------ Title: " + rssFeedItem.getTitle().getType().toString() + 
				  " Description: " + rssFeedItem.getDescription().getType().toString());
			
			if (rssFeedItem.getTitle().getType() == RssFeedTextItemType.TEXT
					& rssFeedItem.getDescription().getType() == RssFeedTextItemType.TEXT)
				vi = inflater.inflate(R.layout.rss_feed_item___text_text, null);
			if (rssFeedItem.getTitle().getType() == RssFeedTextItemType.TEXT
					& rssFeedItem.getDescription().getType() == RssFeedTextItemType.HTML)
				vi = inflater.inflate(R.layout.rss_feed_item___text_html, null);

			if (vi == null)
				vi = inflater.inflate(R.layout.rss_feed_item___text_text, null);
		}

		TextView titleText = (TextView) vi.findViewById(R.id.titleText);
		TextView contentText = (TextView) vi.findViewById(R.id.contentText);
		TextView dateText = (TextView) vi.findViewById(R.id.dateText);

		// Setting all values in listview
		if (rssFeedItem.getTitle() != null
				&& rssFeedItem.getTitle().getContent() != null)
			titleText.setText(rssFeedItem.getTitle().getContent());

		if (rssFeedItem.getDescription() != null
				&& rssFeedItem.getDescription().getContent() != null
				&& rssFeedItem.getDescription().getType() == RssFeedTextItemType.TEXT)
			contentText.setText(rssFeedItem.getDescription().getContent());

		if (rssFeedItem.getPublished() != null)
			dateText.setText(rssFeedItem.getPublished().toString());

		return vi;
	}
}
